import core.ui.Event;
import core.util.Listeners;
import xcms.plugins.editor.Selection; 

class xcms.plugins.editor.Inspector
{
    /**
     * Initializes the inspector.
     */
    this.Inspector = function() {
        this.inspecting = false;
        this.listeners = new core.util.Listeners();
        this.selection = new Selection();
        this.onDocumentMouseOver = bind(this, this.onDocumentMouseOver);
        this.onDocumentMouseDown = bind(this, this.onDocumentMouseDown);
        this.onDocumentClick = bind(this, this.onDocumentClick);
    }

    /**
     * Sets whether to inspect the page or not.
     *
     * @param inspect (boolean) true if so, false otherwise.
     */
    this.setInspecting = function(inspect) {
        this.inspecting = inspect;
        // TODO: use all frames
        // TODO: add/remove key listeners (return/escape/up/down)
        var fn = inspect ? core.ui.Event.addListener : core.ui.Event.removeListener;
        fn(document, 'mouseover', this.onDocumentMouseOver);
        fn(document, 'mousedown', this.onDocumentMouseDown);
        fn(document, 'click', this.onDocumentClick);
        if (!inspect) {
            this.selection.select(null);
        }
    }

    /**
     * Gets whether the inspector is currently working.
     *
     * @return true if so, false otherwise
     */
    this.isInspecting = function() {
        return this.inspecting;
    }

    /**
     * Registers new selection listener.
     *
     * @param listener (object) Listener object to register.
     */
    this.addSelectionListener = function( listener )
    {
        this.listeners.add(listener);
    }

    /**
     * Unregisters the given listener.
     *
     * @param listener (object) Listener object to unregister.
     */
    this.removeSelectionListener = function( listener )
    {
        this.listeners.remove(listener);
    }

    /**
     * Fires when the particular DOM element has been selected.
     */
    this.fireElementSelected = function()
    {
        this.listeners.call( 'onElementSelected', [ this ] );
    }

    /**
     * @return HTMLElement Currently selected page element.
     */
    this.getSelectedElement = function()
    {
        return this.selection.getSelectedElement();
    }

    this.onDocumentMouseOver = function(event) {
        // TODO: get rid of need to always "improve" events, do it automatically
        event = core.ui.Event.improve(event); 
        this.inspect(event.getTarget());
        event.stop();
        event.cancel();
    }

    this.onDocumentMouseDown = function(event) {
        this.fireElementSelected();
        this.setInspecting(false);
        event = core.ui.Event.improve(event);
        event.cancel();
        event.stop();
    }

    this.onDocumentClick = function(event) {
        this.setInspecting(false);
        event = core.ui.Event.improve(event);
        event.cancel();
        event.stop();
    }

    /**
     * @private
     * @param element (HTMLElement) Element to inspect.
     */
    this.inspect = function(element) {
        if (element && ( 1 != element.nodeType)) {
            element = element.parentNode;
        }
        if (element && element.winid) {
            // Just one of XCMS controls
            return;
        }
        this.selection.select(element);
    }
}